var key = 'mytinytinyrss';
var sid = '';

function loadOptions() {

	var config = localStorage[key];
	if (config != undefined) {
		config = config.split(" ");
		$('#url').val(config[0]);
		$('#username').val(config[1]);
		$('#password').val(config[2]);
		$('#sid').html(config[3]);
	}
}

function login() {
	$.post($('#url').val(), '{"op":"login","user":"' + $('#username').val() + '","password":"' + $('#password').val() + '"}',function(data){
		//console.log(data)
		sid = data['content']['session_id'];
		$('#sid').val(sid);
		var config = $('#url').val() + " " + $('#username').val() + " " + $('#password').val() + " " + sid;
		localStorage[key] = config;
	}, 'json');
}

function saveOptions() {

	if ('' == sid)
		login();
}

function eraseOptions() {
	localStorage.removeItem(key);
	location.reload();
}

document.addEventListener('DOMContentLoaded', function () {	
	loadOptions();
	$('#save').click(saveOptions);
	$('#restore').click( eraseOptions);
});